package com.video.controller;

import com.video.annotation.InvokeAn;
import com.video.dto.VlogDTO;
import com.video.result.ResultVO;
import com.video.entity.Vlogs;
import com.video.result.YesOrNo;
import com.video.service.VlogsService;
import com.video.vo.PublishVlogVo;
import com.video.vo.QueryVlogsVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

/**
 * @author baoshuai
 * @date 2022/12/14
 */
@Api(tags = "短视频模块接口")
@RestController
@RequestMapping("/vlogs")
public class VlogsController {

    @Resource
    private VlogsService vlogsService;

    @ApiOperation("根据id删除短视频")
    @DeleteMapping("/{id}")
    public ResultVO deleteById(@PathVariable Long id) {
        vlogsService.removeById(id);
        return ResultVO.success();
    }

    @ApiOperation("分页条件查询")
    @GetMapping("/pageList")
    @ApiImplicitParams({
            @ApiImplicitParam(dataType = "int", name = "pageNum", value = "当前页", required = true),
            @ApiImplicitParam(dataType = "int", name = "pageSize", value = "每页大小", required = true),
            @ApiImplicitParam(dataType = "string", name = "keyword", value = "搜索关键字", required = false),
    })
    public ResultVO pageList(Integer pageNum, Integer pageSize, String keyword) {
        return vlogsService.pageList(pageNum, pageSize, keyword);
    }

    @InvokeAn
    @ApiOperation("根据id查询短视频及点赞信息")
    @GetMapping("/{id}")
    public ResultVO getVlogById(@PathVariable Long id, HttpServletRequest request) {
        String token = request.getHeader("token");
        QueryVlogsVo queryVlogsVo = vlogsService.queryVlogAndLikeInfo(id, token);
        return ResultVO.success(queryVlogsVo);
    }

    @ApiOperation("发布短视频")
    @PostMapping("/publish")
    public ResultVO publishVlog(@RequestBody PublishVlogVo publishVlogVo) {
        return vlogsService.publishVlog(publishVlogVo);
    }

    @ApiOperation("设为私密")
    @PutMapping("/changeToPrivate/{userId}/{vlogId}")
    public ResultVO changeToPrivate(@PathVariable Long userId,
                                    @PathVariable Long vlogId) {
        return vlogsService.changePrivateOrPublic(userId, vlogId, YesOrNo.YES.type);
    }

    @ApiOperation("设为公开")
    @PutMapping("/changeToPublic/{userId}/{vlogId}")
    public ResultVO changeToPublic(@PathVariable Long userId,
                                   @PathVariable Long vlogId) {
        return vlogsService.changePrivateOrPublic(userId, vlogId, YesOrNo.NO.type);
    }

    @ApiOperation("修改视频标题")
    @PutMapping("/modifyTitle")
    public ResultVO modifyTitle(@RequestBody VlogDTO vlogDTO) {
        return vlogsService.modifyTitle(vlogDTO);
    }

    @ApiOperation("修改视频信息")
    @PutMapping("/modifyVlog")
    public ResultVO modifyVlog(@RequestBody VlogDTO vlogDTO) {
        return vlogsService.modifyVlog(vlogDTO);
    }

    @ApiOperation("取消点赞视频")
    @PostMapping("/unlike/{userId}/{vlogId}")
    public ResultVO unlikeVlog(@PathVariable Long userId,
                               @PathVariable Long vlogId) {
        vlogsService.unlikeVlog(userId, vlogId);

        return ResultVO.success();
    }

    @ApiOperation("点赞视频")
    @PostMapping("/like/{userId}/{vlogId}")
    public ResultVO likeVlog(@PathVariable Long userId,
                               @PathVariable Long vlogId) {
        vlogsService.likeVlog(userId, vlogId);

        return ResultVO.success();
    }
}
